{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The below is an example of how to run some benchmarking routines from within Pyquil. The first is conjugating a Pauli by a Clifford element, and the second is constructing randomized benchmarking sequences."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquil.quil import Program\n",
    "from pyquil.api import CompilerConnection\n",
    "from pyquil.gates import CNOT, X,Z, Y, PHASE, H\n",
    "from pyquil.paulis import PauliTerm\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "cc = CompilerConnection()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "clifford = Program().inst(X(1000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pauli = PauliTerm(\"Y\", 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-1+0j)*Y1000\n"
     ]
    }
   ],
   "source": [
    "print(cc.apply_clifford_to_pauli(clifford, pauli))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "clifford = Program().inst(X(0), CNOT(0, 1), Y(1), Z(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "pauli = PauliTerm(\"Y\", 0) * PauliTerm(\"Z\", 1, 1.j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1j*Y0*X1\n"
     ]
    }
   ],
   "source": [
    "print(cc.apply_clifford_to_pauli(clifford, pauli))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "gateset_1q = [PHASE(np.pi/2, 0), H(0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "progs = cc.generate_rb_sequence(100, 1, gateset_1q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H 0\n",
      "PHASE(pi/2) 0\n",
      "H 0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(progs[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(progs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "gateset_2q = [PHASE(np.pi/2, 0), H(0), CNOT(0, 1), H(1), PHASE(np.pi/2, 1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PHASE(pi/2) 0\n",
      "CNOT 0 1\n",
      "H 1\n",
      "CNOT 0 1\n",
      "H 0\n",
      "PHASE(pi/2) 0\n",
      "H 1\n",
      "CNOT 0 1\n",
      "H 0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "progs_2q = cc.generate_rb_sequence(100, 2, gateset_2q)\n",
    "print(progs_2q[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
